package com.zhongshan.ga.mobile.logic;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.SqlSession;
import org.restlet.representation.Representation;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongshan.ga.commonvar.ArgsConfig;
import com.zhongshan.ga.commonvar.CommendConfig;
import com.zhongshan.ga.commonvar.PHPWebUrl;
import com.zhongshan.ga.logger.GenerateLog;
import com.zhongshan.ga.mybatis.bean.ActivityInfo;
import com.zhongshan.ga.mybatis.bean.ShareInfoBean;
import com.zhongshan.ga.utils.MyBatisUtil;

/**
 * 获取分享列表的逻辑处理类
 * @author yanqi
 *
 */
public class GetShareInfoListLogic extends ParentLogic {
	/**
	 * 获得分享列表
	 * @param representation
	 * @return
	 */
	public JsonObject getShareInfoList(Representation representation) {
		JsonObject resultJson = new JsonObject();
		Gson gson = new Gson();
		try {
			// 获取请求中的json
			String requestText = representation.getText();
			GenerateLog.outDebug(requestText);
			JsonObject acceptJson = new JsonParser().parse(requestText)
					.getAsJsonObject();
			GenerateLog.outDebug(acceptJson);
			
			/**
			 * 进行基本的逻辑较验
			 */
			resultJson = super.basicCheck(acceptJson, CommendConfig.SHARE_INFO_LIST, false);
			
			if (resultJson.get(ArgsConfig.RESULT_CODE).getAsString()
					.equals(ArgsConfig.RESPONSE_SUCCESS)) {
				// 调用接口 获取数据
				SqlSession session = MyBatisUtil.getSqlSession(true);
				// 获得请求的数据体
				 JsonObject dataJsonObject = acceptJson.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(dataJsonObject);
				// 获得请求的记录下标
				int startIndex = dataJsonObject.get(ArgsConfig.START_INDEX).getAsInt();
				int endIndex = dataJsonObject.get(ArgsConfig.RECORD_NUM).getAsInt();
				// 设置条件参数
				Map<String, Object> paramMap = new HashedMap();
				paramMap.put("startIndex", startIndex);
				paramMap.put("endIndex", endIndex);
				
				/**
				 * 获得数据
				 */
				List<ShareInfoBean> shareInfoList = session.selectList("com.mybatis.mapping.shareInfoMapper.selectShareInfoList", paramMap);
				if (shareInfoList != null) {
					for (ShareInfoBean item : shareInfoList) {
						item.setShareCover(PHPWebUrl.SHARE_INFO_IMAGE_URL
								+ item.getShareCover());
					}
				}
				/**
				 * 获得每次查询的记录总数
				 */
				String reObject = session.selectOne("com.mybatis.mapping.shareInfoMapper.selectShareInfoCount", paramMap);
				if(reObject == null) {
					reObject = "0";
				}
				GenerateLog.outDebug(reObject.toString());
				GenerateLog.outDebug("****************************结果数据为："+ shareInfoList.toString() +"**********************");
				JsonParser jsonParser = new JsonParser();
				JsonArray jsonArray = jsonParser.parse(gson.toJson(shareInfoList)).getAsJsonArray();
				GenerateLog.outDebug("****************************结果数据为："+ jsonArray +"**********************");
				JsonObject tempJsonObject = acceptJson.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(tempJsonObject);
				
				tempJsonObject.add(ArgsConfig.RESULT_DATA, jsonArray);
				GenerateLog.outDebug(tempJsonObject+"11111111111111111");
				resultJson.add(ArgsConfig.REQUEST_DATA, acceptJson.get(ArgsConfig.REQUEST_DATA).getAsJsonObject());
				resultJson.addProperty(ArgsConfig.TOTAL_RECORD_NUM_EVERY_TIME, Integer.valueOf(reObject));
				GenerateLog.outDebug("***************************返回的数据：" + resultJson + "**********************************");
			}
			return resultJson;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}
